#coding: utf-8
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.files import upload_template

#########################
# SCRIPT UPLOAD EXAMPLE #
#########################
#
#
#


def ubuntu_install():
    '''
    1 # new installation for Ubuntu server
    '''
    upload_ubuntu_installation_script()
    print('')

def upload_ubuntu_installation_script():
    '''
    # 1 upload setup-web2py-ubuntu.sh script to target
    '''
    filename    = env.source_script_ubuntu
    destination = env.target_script_ubuntu
    upload_template(filename=filename,         # path to text file
                    destination=destination,   # target destination (path) of rendered file
                    context=None,              # file render context dictionary
                    use_jinja=False,
                    template_dir=None,         # if jinja True looks here
                    use_sudo=False)
    run('cat %s' %destination)

def wrapper_conf_template_load():
    '''
    # X upload a test template to target
    '''
    env.wrapper_conf = dict(
        USER  = env.user,
        VIRTUALENVS_HOME = env.target_envs,
    )
    filename     = env.source_template_wrapper_config
    destination  = env.target_template_wrapper_config
    template_dir = env.source_template_wrapper_config_dir
    upload_template(filename=filename,         # path to text file
                    destination=destination,   # target destination (path) of rendered file
                    context=env.wrapper_conf,          # file render context dictionary
                    use_jinja=True,
                    template_dir=template_dir, # if jinja True looks here
                    use_sudo=False)
    run('cat %s' %destination)

####################
# TEMPLATE EXAMPLE #
####################
#
#
#
def test():
    '''
    # X upload a test template to target
    '''
    env.hgrc = dict(
        COMMIT_USER  = 'Christopher Steel',
        COMMIT_EMAIL = 'Christopher.Steel@VoiceOfAccess.org',
        REPOSITORY   = 'uc-deploy',
        REPOSITORY_URL = '',
        REPOSITORY_PASSWORD = 'password',
    )
    filename     = env.source_template_test
    destination  = env.target_template_test
    template_dir = env.source_template_dir
    upload_template(filename=filename,         # path to text file
                    destination=destination,   # target destination (path) of rendered file
                    context=env.hgrc,          # file render context dictionary
                    use_jinja=True,
                    template_dir=template_dir, # if jinja True looks here
                    use_sudo=False)
    run('cat %s' %destination)